const router = require("koa-router")();

router.prefix("/gps");

const PagerHelper = require("./PagerHelper");

var client = require("../db/momo/MonoDb");

router.get("/", async (ctx, next) => {
  var { carNum='' } = ctx.request.query;

  var result = await new Promise((resolve, reject) => {
    client.connect(function (err) {
      if (err) throw err;
      var dbo = client.db("runoob");
      var q = {
        carNum: carNum,
      };

      dbo
        .collection("gps")
        .find(q)
        .sort({positionTime:-1})
        .toArray((err, docs) => {
          if (err) throw reject(err);
          resolve(docs);
        });
    });
  });

  ctx.body = PagerHelper.SuccessRes(result);
});

router.get("/save", async (ctx, next) => {
  var Func = require("./Func");

  var result = await Func.UserMonitorByAPI();

  ctx.body = PagerHelper.SuccessRes(result);
});

module.exports = router;
